package com.bookmanager.www.common.exception;


import com.bookmanager.www.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

/**
 * @author 风离
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

//    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(value = ShiroException.class)
    @ResponseBody
    public Result handler(ShiroException e) {
        log.error("Shiro异常：----------------{}", e);
        return Result.fail("产生了Shiro异常", e.getMessage());
    }

    public Result unknownAccountException(UnknownAccountException unknownAccountException)
    {
        log.info("[ JWT异常捕获 ] " + unknownAccountException.getMessage());

        return Result.fail("",unknownAccountException.getMessage());
    }

//    @ResponseStatus(HttpStatus.BAD_REQUEST)
//    @ExceptionHandler(value = MethodArgumentNotValidException.class)
//    public Result handler(MethodArgumentNotValidException e) {
//        log.error("实体校验异常：----------------{}", e);
//        BindingResult bindingResult = e.getBindingResult();
//        ObjectError objectError = bindingResult.getAllErrors().stream().findFirst().get();
//
//        return Result.fail("实体校验异常",objectError.getDefaultMessage());
//    }
//

    @ResponseBody
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result throwCustomException(MethodArgumentNotValidException methodArgumentNotValidException) {

        log.info("[ @Vaild异常捕获 ] " + methodArgumentNotValidException.getMessage());

        return Result.fail("",methodArgumentNotValidException.getBindingResult().getFieldError().getDefaultMessage());
    }

//    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    @ExceptionHandler(value = IllegalArgumentException.class)
    public Result handler(IllegalArgumentException e) {
        log.error("Assert异常：----------------{}", e);
        return Result.fail("Assert异常",e.getMessage());
    }

//    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    @ExceptionHandler(value = RuntimeException.class)
    public Result handler(RuntimeException e) {
        log.error("运行时异常：----------------{}", e);
        return Result.fail("运行时异常",e.getMessage());
    }

    @ResponseBody
    @ExceptionHandler(NullPointerException.class)
    public Result handler(NullPointerException e) {
        log.error("空指针异常：----------------{}", e);
        return Result.fail("空指针异常",e.getMessage());
    }

//    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Result handler(Exception e) {
        log.error("其他异常：----------------{}", e);
        return Result.fail("其他异常",e.getMessage());
    }

}
